<template>
  <div class="hello">
    <!-- 第一行 -->
    <div class="row row1">
      <ul class="grid width20 one">
        <li class="content-center center" v-for="(top, key, index) in top3top" :key="index">
          <section class="name-word">
            <span class="name">{{ key }}</span>
            <span class="num">{{ dformat(top) }}</span>
          </section>
        </li>
      </ul>
      <!-- 第二列 选项卡 -->
      <div class="grid width45">
        <div class="grid-title">园区注册企业概况{{ num }}</div>
        <div class="grid-body">
          <div class="height">
            <ul class="chart-tab">
              <li
                v-for="(item,index) in tabs"
                :key="index"
                :class="{active:index == num}"
                @click="tab(index)"
              >企业行业分析</li>
            </ul>
            <ul class="height-tab">
              <transition
                enter-active-class="animated fadeInLeft"
                leave-active-class="animated fadeOutLeft"
              >
                <li v-show="num == 0">
                  <div id="chartsPie0" class="block"></div>
                </li>
              </transition>
              <transition
                enter-active-class="animated fadeInLeft"
                leave-active-class="animated fadeOutLeft"
              >
                <li v-show=" num == 1">
                  <div id="chartsPie1" class="block"></div>
                </li>
              </transition>
              <transition
                enter-active-class="animated fadeInRight"
                leave-active-class="animated fadeOutRight"
              >
                <li v-show=" num == 2">
                  <div id="chartsPie2" class="block"></div>
                </li>
              </transition>
            </ul>
          </div>
        </div>
      </div>
      <div class="grid width35">
        <div class="grid-title">园区企业注册资本</div>
        <div class="grid-body">
          <div class="block" id="chartsPie3"></div>
        </div>
      </div>
    </div>
    <!-- 第2行 -->
    <div class="row row2">
      <div class="grid width65 big">
        <div class="contain">
          <div class="width100 height50 top">
            <div class="grid-title">投资带动企业概况</div>
            <div class="grid-body">
              <div class="flex height">
                <div class="topflex">
                  <ul class="data1 height">
                    <li
                      v-for=" (val, key, index) in compare(persort)"
                      v-if=" key>= 0 && key<3 "
                      :key="index"
                    >
                      <div v-for="(v,k) in val" :key="k">
                        <em>{{key}}</em>
                        {{ k }}
                        <i class="text-right">{{ v }}%</i>
                      </div>
                    </li>
                  </ul>
                </div>
                <!-- 柱状图 -->
                <div class="topflex">
                  <div class="block">
                    <div id="drawbar" class="block"></div>
                  </div>
                </div>
                <div class="topflex">
                  <div class="data3 height">
                    <div id="chartsPie4" class="block"></div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="width100 height50 bottom">
            <div class="grid-title">周边带动企业概况</div>
            <div class="grid-body">
              <div class="flex height">
                <div class="topflex">
                  <ul class="data2 height">
                    <li
                      v-for=" (val, key, index) in compare(persort)"
                      v-if="key >= 0 && key<3"
                      :key="index"
                    >
                      <div v-for="(v,k) in val" :key="k">
                        <em>{{key}}</em>
                        {{ k }}
                        <i class="text-right">{{ v }}%</i>
                      </div>
                    </li>
                  </ul>
                </div>
                <!-- 柱状图 -->
                <div class="topflex">
                  <div class="block">
                    <div id="drawbar2" class="block"></div>
                  </div>
                </div>
                <div class="topflex">
                  <div class="data3 height">
                    <div id="chartsPie5" class="block"></div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="grid width35">
        <div class="grid-title">园区注册企业概况</div>
        <div class="grid-body">
          <div class="scroll-div">
            <ul :style="{ top }">
              <li v-for="u in company4.OTH00001I" >
                <dl>
                  <dt>{{ u.name }}</dt>
                  <dd>注册资本：{{ u.registeredCapital }} 成立时间：{{ u.establishDate }}</dd>
                </dl>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  mapState,
  mapActions,
  mapMutations,
mapGetters
} from 'vuex'
// 引入echarts
import echarts from 'echarts';
export default {
  data() {
    return {
      tabs: ["标题一", "标题二", "标题三"],
      num: 0,
      activeIndex: 0
    }
  },
  computed: {
    ...mapState({
      companyUrl: state => state.Company.companyUrl,
      loaded: state => state.Company.loaded,
      company: state => state.Company.company,
      company4: state => state.Company.company4,
      line1pie: state => state.Company.line1pie,
      line3pie: state => state.Company.line3pie,
      persort: state => state.Company.persort,
      top3top: state => state.Company.top3top,
    }),
   
    top() {
      return -this.activeIndex * 60 + 'px';
    },
    
  },
  created() {
    this.tabChange();
    this.loadData(this.companyUrl)
  },
  mounted: function () {
    setTimeout(() => {
      this.$nextTick(function () {
        this.drawpie('chartsPie0', this.dataFor(this.line1pie));
        this.drawpie('chartsPie1', this.dataFor(this.line1pie));
        this.drawpie('chartsPie2', this.dataFor(this.line1pie));
        this.drawpie('chartsPie3', this.dataFor(this.line1pie));
        this.drawpie('chartsPie4', this.dataFor(this.line1pie));
        this.drawpie('chartsPie5', this.dataFor(this.line1pie));
        this.drawbar('drawbar', ["#c73b70"]);
        this.drawbar('drawbar2', ["#14b9f6"]);
      });
    }, 500)

  },
  methods: {
    ...mapActions([
      'loadData',
      'dformat'
    ]),
    newin() {
      this.$store.dispatch('timeAdd').then(() => {
        console.log('timeAdd');
      });
    },
    compare(dic) { //排序
      var arr = new Array();
      var sdic = Object.keys(dic).sort(function (a, b) {
        return dic[a] - dic[b]
      });
      for (var ki in sdic) {
        var obj = new Object();
        var key = sdic[ki];
        var val = dic[sdic[ki]]
        obj[sdic[ki]] = val
        arr.push(obj);
      }
      return arr.reverse();
    },
    tabChange() {
      setInterval(() => {
        this.num++;
        if (this.num == 3) {
          this.num = 0;
        }
      }, 3000);
    },
    tab(index) { //轮播
      this.num = index;
    },
    dataFor(obj) {
      var arr = new Array();
      for (var key in obj) {
        var objItem = new Object();
        objItem = {
          name: key,
          value: obj[key]
        }
        arr.push(objItem)
      }
      return arr;
    },
    drawpie(id, datas) {
      let myChart = echarts.init(document.getElementById(id));
      let option = {
        title: {},
        tooltip: {
          trigger: 'item',
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        grid: {
          top: '10',
          left: 0,
          right: 0,
          bottom: 0,
          containLabel: true
        },
        series: [{
          name: '访问来源',
          type: 'pie',
          radius: '70%',
          center: ['50%', '50%'],
          color: ['#ce4f90', '#b8216e', '#a11d61', '#6b1441', '#de467f'],
          data: datas, //数据
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }]
      };
      myChart.setOption(option);
      window.onresize = function () { myChart.resize(); }
      window.addEventListener("resize", function () {
        myChart.resize();
      });

    },
    drawbar(id, barColor) {
      let myChart = echarts.init(document.getElementById(id));
      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          top: '10%',
          left: '0',
          right: '0',
          bottom: '10%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          splitNumber: 3,
          axisLabel: {
            show: true,
            textStyle: {
              color: '#999'
            },
          },
          axisLine: {
            lineStyle: {
              color: '#999'
            }
          },
          splitLine: {
            show: false
          },
          boundaryGap: [0, 0.01]
        },
        yAxis: {
          axisLabel: {
            show: true,
            textStyle: {
              color: '#999'
            }
          },
          axisLine: {
            lineStyle: {
              color: '#999'
            },
          },
          splitLine: {
            show: false
          },
          type: 'category',
          data: ['巴西', '印尼', '美国', '印度', '中国', '世界人口(万)']
        },
        series: [{
          center: ['0%', '0%'],
          type: 'bar',
          data: [19325, 23438, 31000, 121594, 134141, 681807],
          itemStyle: {
            normal: {
              color: barColor
            }
          },
        }]
      };
      myChart.setOption(option);
      window.onresize = function () { myChart.resize(); }
      window.addEventListener("resize", function () {
        myChart.resize();
      });
    },
  },
}
</script>
<style lang="scss" scoped>
.height-tab {
  height: calc(100% - 1.625rem);
  li {
    width: 100%;
    height: 100%;
  }
}
.chart-tab {
  background-color: #161619;
  width: 80%;
  height: 1.625rem;
  margin: 1% auto;
  border-radius: 0.3125rem;
  li {
    width: 33.3%;
    height: 100%;
    border-radius: 0.3125rem;
    float: left;
    font-size: 0.875rem;
    background: #161619;
    line-height: 1.625rem;
    color: #fff;
    text-align: center;
    -ms-flex-line-pack: center;
    align-content: center;
    box-sizing: border-box;    
    cursor:pointer;
    &.active {
      background-color: #1f66f8;
    }
  }
}
.scroll-div {
  position: relative;
  height: 100%;
  top: 0.25rem;
  overflow: hidden;
  ul {
    position: absolute;
    left: 1.25rem;
    top: 0;
    width: 100%;
    height: 100%;
    transition: all 0.25s ease;
    li {
      margin: 10px 0;
      position: relative;
      padding: 0 1.25rem;
      list-style-type: none;
      &:before {
        position: absolute;
        left: 0;
        top: 0;
        width: 0.1875rem;
        height: 100%;
        content: "";
        background: #776efd;
      }
    }
  }
  dl {
    color: #fff;
    font-size: 1rem;
    dd {
      color: #9a9a9a;
      font-size: 0.875rem;
    }
  }
}
.data1,
.data2 {
  padding: 20px;
  font-size: 0.875rem;
  li {
    list-style: none;
    height: 33.33%;
    em {
      display: inline-block;
      font-style: normal;
      width: 22px;
      height: 22px;
      border: 2px solid #c73b70;
      border-radius: 100%;
      text-align: center;
      border-right-color: #242428;
    }
    .text-right {
      float: right;
      color: #c73b70;
      font-style: normal;
    }
  }
}
.data2 {
  li {
    em {
      border: 2px solid #14b9f6;
      border-right-color: transparent;
      color: #14b9f6;
    }
    .text-right {
      color: #14b9f6;
    }
  }
}
</style>
